package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserDiscount;
import com.tr.rentcar.model.dao.UserDiscountDao;

/**
 * Servlet implementation class UserDiscount
 */
@WebServlet("/user/discount")
public class UserDiscountsController extends HttpServlet {
	UserDiscountDao ua=new UserDiscountDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String discountState=request.getParameter("discountstate");
		User user=(User) request.getSession().getAttribute("userinfo");
		List<UserDiscount> uo = new ArrayList<UserDiscount>();//存储过期或未过期优惠卷
		List<UserDiscount> userd = null;//储存全部优惠卷
		try {
			userd = ua.findById(user.getUid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (discountState.equals("1")) {
			request.setAttribute("userd", userd);
			request.setAttribute("titlem", "全部优惠卷");
		}else if(discountState.equals("2")){
				for (UserDiscount userDiscount : userd) {
					if (!userDiscount.getUdsTime().equals("已过期")) {
						uo.add(userDiscount);
					}
				}
				request.setAttribute("userd", uo);
			request.setAttribute("titlem", "可用优惠卷");
		}else if(discountState.equals("3")){
			for (UserDiscount userDiscount : userd) {
				if (userDiscount.getUdsTime().equals("已过期")) {
					uo.add(userDiscount);
				}
			}
			request.setAttribute("userd", uo);
			request.setAttribute("titlem", "已过期优惠卷");
		}
		request.getRequestDispatcher("/WEB-INF/jsp/user_discount.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
